package com.ht.shop.dao.impl;

import com.ht.shop.dao.UserDao;
import com.ht.shop.pojo.TbUser;
import com.ht.shop.utils.DBUtils;
import com.ht.shop.utils.SqlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner = new QueryRunner(DBUtils.getSource());
    @Override
    public void addUser(TbUser user) throws SQLException {
        String sql = SqlUtils.getSql("user.add");
        queryRunner.insert(sql,new ScalarHandler<>(),user.getUsername(),user.getPassword(),user.getNickname(),user.getEmail(),user.getGender(),user.getCode());

    }

    @Override
    public int active(String username, String code) throws Exception {
        String sql = SqlUtils.getSql("user.active");
        return queryRunner.update(sql,username,code);
    }

    @Override
    public TbUser findUserByName(String username) throws Exception {
        String sql = SqlUtils.getSql("user.find.by.name");
        return queryRunner.query(sql,new BeanHandler<>(TbUser.class),username);
    }
}
